package com.bailian.exception;

import com.alibaba.fastjson.JSONObject;
import com.bailian.core.utils.BindExceptionUtil;
import com.bailian.service.BaseApiService;
import com.bailian.service.BaseResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler extends BaseApiService<JSONObject> {

    @ExceptionHandler(MethodArgumentNotValidException.class)
    @ResponseBody
    public BaseResponse<JSONObject> constraintViolationException(MethodArgumentNotValidException e) {
        log.info("###全局参数校验异常###,error:{}", e);
        return setResultError(BindExceptionUtil.getMessage(e));
    }

    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public BaseResponse<JSONObject> exceptionHandler(RuntimeException e) {
        log.info("###全局运行时异常###,error:{}", e);
        return setResultError("系统错误!");
    }
}